<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" id="inputId" />
		<button onclick="cal()">阶乘</button>
		=
		<span id="resultId"></span>
		
		<script type="text/javascript">
			function cal() {
				var num = document.getElementById('inputId').value;
				var resultObj = document.getElementById('resultId');
				/* resultObj.innerText = '<span style="color:red">不能为空</span>'; */
				/* if (num == '') {
					resultObj.innerHTML = '<span style="color:red">不能为空</span>';
					return;
				} */
				// NaN:Not a number
				/* if (isNaN(num)) {
					resultObj.innerHTML = '<span style="color:red">您输入的不是一个数字</span>';
					return;
				} */
				if (num == '' || isNaN(num)) {
					resultObj.innerHTML = '<span style="color:red">不能为空 / 您输入的不是一个数字</span>';
					return;
				}
				
				if (num < 0) {
					resultObj.innerHTML = '<span style="color:red">负数没有阶乘</span>';
					return;
				}
				if (num == 0) {
					resultObj.innerText = 1;
					return;
				} 
				
				// n!=n*(n-1)*(n-2)...*3*2*1
				var result = 1;
				for (var i = num; i >= 1; i--) {
					result = result * i;
				}
				resultObj.innerText = result;
			}
		</script>
	</body>
</html>
